package com.zerui.manager.app.accessauthdevice.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zerui.cola.dto.Response;
import com.zerui.manager.accessauthdevice.domainservice.AccessAuthDeviceRepository;
import com.zerui.manager.accessauthorization.AccessAuth;
import com.zerui.manager.accessauthorization.gateway.AccessAuthGateWay;
import com.zerui.manager.web.cmd.AccessAuthDeviceBindCmd;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

/**
 * @Author wh
 * @create_time 2023-08-29 14:35
 */
@Component
@CatchAndLog
public class AccessAuthDeviceDelCmdExe {

    @Resource
    private AccessAuthDeviceRepository accessAuthDeviceRepository;
    @Resource
    private AccessAuthGateWay accessAuthGateWay;

    public Response execute(AccessAuthDeviceBindCmd cmd) {
        AccessAuth accessAuth = accessAuthGateWay.selectById(cmd.getAccessAuthId());
        if(ObjectUtils.isEmpty(accessAuth)){
            throw new BizException("门禁授权不存在");
        }
        accessAuthDeviceRepository.devicesDeleteDistributePermissions(cmd.getAccessAuthId(),cmd.getDeviceIds());
        return Response.buildSuccess();
    }

    public void executeDel(Long deviceId) {
        accessAuthDeviceRepository.deleteByDeviceId(deviceId);
    }
}
